package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ContractOriginal;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.rContractListMain;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lenovo on 2016/6/1.
 */
public class SingleContractListFragment extends BaseListFragment {
  private BaseListAdapter mAdapter;
  private CurrentUser mCurrentUser;
  private String mManageCode;

  @Override public void onCreate(Bundle savedInstanceState) {
    mCurrentUser = CurrentUser.newInstance(getActivity());
    // 差旅费用报销详情参数
    //        mManageCode=getArguments().getString(AddTravelReimburseDetailFragment.EXTRA_CODE);
    super.onCreate(savedInstanceState);
  }

  //    @Override
  //    public String getUrl() {
  //        return ApiConst.URL_CONTRACT;
  //    }
  //    @Override
  //    public HashMap<String, String> getparameters() {
  //
  //        HashMap<String, String> params = new HashMap<String, String>();
  //        params.put("curPage", "1");
  //        params.put("pageSize", "2000");
  //        params.put("style", "tree");
  //        params.put("memberCode",mCurrentUser.getMemberCode());
  //        params.put("certificateCode", mManageCode.toString());
  //        params.put("columnList", "ID,CONTRACT_NAME,CONTRACT_KIND,CONTRACT_CODE");
  //        params.put("dataStatus", "checkPass");
  //        return params;
  //
  //    }
  @Override public String getUrl() {
    return ApiConst.URL_CONTRACT_LIST_RECEIPT;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    String memberCode = CurrentUser.newInstance(getActivity()).getMemberCode();
    params.put("curPage", "1");
    params.put("pageSize", "10000");
    params.put("memberCode", memberCode);
    params.put("ifAllpay", "");
    params.put("ifInvoiceFinished", "");
    params.put("partId", "");
    params.put("dataStatus", "checkPass");
    params.put("orderField", "order_no");
    params.put("orderSeq", "desc");
    params.put("type", "mobile");

    return params;
  }

  @Override public void resolveResponse(String arg0) {

    ContractOriginal attendanceListOriginal = new Gson().fromJson(arg0, ContractOriginal.class);

    List<rContractListMain> dataList = attendanceListOriginal.getRows();
    setAdapter(dataList);
  }

  private void setAdapter(List<rContractListMain> detail) {
    if (detail == null) {
      detail = new ArrayList<rContractListMain>();
    }

    if (mAdapter == null) {
      mAdapter = new BaseListAdapter<rContractListMain>(getActivity(), detail) {

        @Override
        public void bindData(int position, BaseListAdapter.ViewHolder vh, rContractListMain item) {
          vh.mTitleOneTv.setText(getContext().getString(R.string.contractNo));
          vh.mContentOneTv.setText(item.getContractCode());//编码
          vh.mTitleTwoTv.setText(getContext().getString(R.string.contractName));
          vh.mContentTwoTv.setText(item.getContractName());//名称
          //                    vh.mTitleThreeTv.setText(getContext().getString(R.string.contractKind));
          //                    vh.mContentTwoTv.setText(item.getCONTRACT_KIND());//类别
        }
      };
      setListAdapter(mAdapter);
      return;
    }

    mAdapter.clear();
    mAdapter.addAll(detail);
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {

    rContractListMain item = (rContractListMain) mAdapter.getItem(position - 1);
    CustomerDialog content =
        new CustomerDialog(item.getContractName(), item.getId(), item.getContractKind(),
            item.getContractCode(), item.getContractAmount() + "");
    Intent i = new Intent();
    i.putExtra(ContractChoosePagerActivity.EXTRA_CONTRACT, content);
    i.putExtra(ContractChoosePagerActivity.EXTRA_TYPE, ContractChoosePagerActivity.CONTRACT);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  public void refresh() {
    volleyPost();
  }

  public static SingleContractListFragment newInstance(String mManageCode) {
    Bundle args = new Bundle();
    args.putString(AddTravelReimburseDetailFragment.EXTRA_CODE, mManageCode);
    SingleContractListFragment fragment = new SingleContractListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
